<template>
    <div id="loginBox">
        <el-form :model="loginForm" status-icon :rules="loginRules" ref="ruleForm" label-width="100px" class="demo-ruleForm" id="loginForm">
            <el-form-item label="账号" prop="username">
                <el-input type="text" v-model="loginForm.username" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="login()">提交</el-button>
                <el-button >重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data(){

            return{
                loginForm:{username:"",password:""},
                loginRules:{
                    username:[
                        { required: true, message: '账号不能为空', trigger: 'blur' },
                    ],
                    password:[
                        { required: true, message: '密码不能为空', trigger: 'blur' },
                    ],
                }
            }
        },
        methods:{
            login(){
                this.$http.post("http://localhost:8001/login",this.loginForm).then(resp=>{
                    if(resp.data.code===200){
                        //获取服务器响应的token值。保存LocalStorage
                        localStorage.setItem("token",resp.data.data);
                        //跳转路由 不能使用location.href
                        this.$router.push("/Student")
                    }else{
                        this.$message.error("账户或密码错误")
                    }
                })
            }
        }
    }
</script>

<style >
    #loginBox{
        width: 600px;
        height: 300px;
        border: 5px solid deepskyblue;
        margin: 150px auto;
    }
    #loginForm{
        margin: 40px auto;
    }
</style>